<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" @click="addVisible = true" size="mini"
          >添加分类</el-button
        >
      </div>
      <el-table :data="ArtCateList" border stripe style="width: 100%">
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作" width="280">
          <template v-slot="{row}">
            <el-button type="primary" @click="editArtCate(row.id)" round>修改</el-button>
            <el-button type="danger" @click="delArtCate(row.id)" round>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加弹出层 -->
    <el-dialog  @closed="$refs.addForm.resetFields()" title="添加文章分类" :visible.sync="addVisible" width="30%">
        <!-- 表单 -->
      <el-form ref="addForm" :rules="addRules" :model="addForm" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addArtCate"
          >添 加</el-button
        >
      </span>
    </el-dialog>
    <!-- 修改弹出层 -->
    <el-dialog  @closed="$refs.editForm.resetFields()" title="修改文章分类" :visible.sync="editVisible" width="30%">
        <!-- 表单 -->
      <el-form ref="editForm" :rules="addRules" :model="editForm" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editArtCateinfo"
          >修 改</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
  name: 'ArtCate',
  data () {
    return {
      list: [],
      addVisible: false,
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称为1到10位的非空字符串', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  computed: {
    ...mapState('artCate', ['ArtCateList'])
  },
  methods: {
    ...mapActions('artCate', ['getList']),
    // 添加功能
    addArtCate () {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return this.$message.error('请正确填写信息')
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getList()
        this.addVisible = false
      })
    },
    // 修改功能
    editArtCateinfo () {
      this.$refs.editForm.validate(async valid => {
        if (!valid) return this.$message.error('请正确填写信息')
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getList()
        this.editVisible = false
      })
    },
    // 根据id渲染修改弹出层
    async editArtCate (id) {
      if (id === 1 || id === 2) return this.$message.error('前两个不允许修改哦')
      this.editVisible = true
      const { data: res } = await this.$http.get('/my/cate/info', { params: { id } })
      //   console.log(res)
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.editForm = res.data
    },
    // 根据id删除
    async delArtCate (id) {
      if (id === 1 || id === 2) return this.$message.error('前两个不允许删除哦')
      const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
      //   console.log(res)
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
